'use strict';
const db = uniCloud.database()

const dbCmd = db.command
const uniID = require('uni-id')
exports.main = async (event, context) => {
	const collection = db.collection('file')
	const { uniIdToken } = event
	const payload = await uniID.checkToken(uniIdToken)
	if (payload.code > 0) {
		return {
			code: 1001,
			msg: '用户身份验证失败，请重新登录'
		}
	}
	console.log(payload)
	let res =null;
	if(event.action=="add"){
		event.params.uid=payload.uid
		res = await collection.add(event.params)
	}else if(event.action=="get"){
		res = await collection.where({uid: payload.uid}).get()
	}else if(event.action=="upd"){
		res = await collection.where({uid: payload.uid,_id:event.params._id}).update({
		    'name': event.params.name
		})
	}else if(event.action=="del"){
		res = await collection.where({uid: payload.uid,_id:dbCmd.in(event.params.ids)}).remove()
	}
	return res
};